<template>
	<view>
		<u-navbar :is-back="true" :is-fixed="true" title="账号信息"></u-navbar>
		<free-list-item title="我的头像" :showRight="false" :showLeftIcon="false" @click="update('navatar')">
			<free-avater slot="right" :src="user.navatar" size="100" style="border-radius: 100%;"></free-avater>
		</free-list-item>
		<free-list-item title="名字" :showRight="false" :showRightIcons="false" :showLeftIcon="false">
			<text slot="right" class="font-14 text-muted">{{user.nickname}}</text>
		</free-list-item>
		<free-list-item title="绑定手机号" :showRight="false" :showLeftIcon="false" @click="updates()">
			<text slot="right" class="font-14 text-muted ursrRight">sss </text>
		</free-list-item>
		
		<free-list-item title="登录密码" :showRight="false" :showLeftIcon="false" @click="update('username')">
			<text slot="right" class="font-14 text-muted ursrRight">{{user.username}}</text>
		</free-list-item>
		<!-- 修改名字|修改微信号 弹框-->
		<free-confirm ref="confirm" :title="confirmTitle">
			<u-field v-model="confirmText" label="原密码" placeholder="请输入原密码">
			</u-field>
			<u-field v-model="confirmText" label="新密码" placeholder="请输入新密码">
			</u-field>
			<u-field v-model="confirmText" label-width="180" label="确认新密码" placeholder="请再次输入密码">
			</u-field>
		</free-confirm>
		<!-- 修改手机号码 弹框-->
		<free-confirm ref="confirmShow" :title="confirmPhomTitle">
			<u-field v-model="confirmText" label="手机号码" placeholder="请输入所绑定的手机号">
			</u-field>
			<u-field v-model="confirmText" label-width="90" label="验证码" placeholder="请输入验证码">
				<u-button size="mini" slot="right" type="warning" @click="getCode">{{codeText}}</u-button>
			</u-field>
			<u-toast ref="uToast"></u-toast>
			<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
				@change="codeChange"></u-verification-code>
		</free-confirm>
	</view>
</template>

<script>
	import freeListItem from "@/components/free-ui/free-list-item.vue";
	import freeAvater from "@/components/free-ui/free-avater.vue";
	import freeDivider from "@/components/free-ui/free-divider.vue";
	import freeConfirm from "@/components/free-ui/free-confirm.vue";
	export default {
		components: {
			freeListItem,
			freeAvater,
			freeDivider,
			freeConfirm
		},
		data() {
			return {
				user: {
					navatar: "../../../static/logo.png",
					nickname: "XGW",
					username: "xaiwei268"
				},
				codeText: '',
				confirmText: "",
				confirmType: "",
				mobile: '', // 手机号
				code: '', // 验证码
				seconds: 60,
			}
		},
		computed: {
			confirmTitle() {
				return this.confirmType == 'username' ? '修改密码' : '修改昵称'
			},
			confirmPhomTitle() {
				return this.confirmType == 'username' ? '更换手机号' : '更换手机号'
			},
			placeholder() {
				return this.confirmType == 'username' ? '输入账号' : '输入名称'
			}
		},
		methods: {
			codeChange(text) {
				this.codeText = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.$u.toast('倒计时结束');
			},
			start() {
				this.$u.toast('倒计时开始');
			},
			update(key) {
				switch (key) {
					case 'navatar':
						uni.chooseImage({
							count: 1,
							sizeType: ['compressed'],
							success: (res) => {
								// 上传
								this.user.navatar = res.tempFilePaths[0]
							}
						})
						break;
					default:
						this.confirmType = key
						this.confirmText = this.user[key]
						this.$refs.confirm.show((close) => {
							if (this.confirmText === '') {
								return uni.showToast({
									title: "不能为空",
									icon: 'none'
								})
							}
							this.user[key] = this.confirmText
							uni.showToast({
								title: "修改成功",
								icon: 'none'
							})
							close()
						})
						break;
				}
			},
			updates(key) {
				switch (key) {
					case 'navatar':
						uni.chooseImage({
							count: 1,
							sizeType: ['compressed'],
							success: (res) => {
								// 上传
								this.user.navatar = res.tempFilePaths[0]
							}
						})
						break;
					default:
						this.confirmType = key
						this.confirmText = this.user[key]
						this.$refs.confirmShow.show((close) => {
							if (this.confirmText === '') {
								return uni.showToast({
									title: "不能为空",
									icon: 'none'
								})
							}
							this.user[key] = this.confirmText
							uni.showToast({
								title: "修改成功",
								icon: 'none'
							})
							close()
						})
						break;
				}
			},
			openCode() {
				uni.navigateTo({
					url: '/pages/my/code/code'
				})
			}
		}
	}
</script>

<style>
</style>